package com.mouliu.base.syn006;

/**
 * 使用synchronized代码块加锁,比较灵活
 * @author liu.mou
 */
public class ObjectLock {
    public void method1(){
        synchronized (this){
            try {
                System.out.println("do method1");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void method02(){
        synchronized (ObjectLock.class){
            try {
                System.out.println("do method2..");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    private  Object lock = new Object();
    public  void method3(){
        synchronized (lock){
            try {
                System.out.println("do method3..");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        final ObjectLock objectLock = new ObjectLock();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                objectLock.method1();
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                objectLock.method02();
            }
        });
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                objectLock.method3();
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}
